package crud;

import java.math.BigDecimal;

import javax.persistence.EntityManager;

import entities.Doctor;
import entities.Employee;
import entities.User;

public class DoctorCRUD extends AbstractGRUD<Doctor> {
	public DoctorCRUD() {
		super(Doctor.class);
	}

	// Tao Doctor theo idEmployee
	public String create(BigDecimal charges, int idEmployee) {
		EntityManager em = this.getEm();
		String result = "success";
		try {
			EmployeeCRUD empCrud = new EmployeeCRUD();
			Employee employee = empCrud.readById(idEmployee);
			if (employee != null) {
				em.getTransaction().begin();
				Doctor doctor = new Doctor(charges, employee);
				em.persist(doctor);
				em.getTransaction().commit();
			} else {
				result = "can't read employee";
			}
		} catch (Exception e) {
			result = e.getMessage();
			em.getTransaction().rollback();
		}
		return result;
	}

	
	// Tao Doctor, Employee, User cung luc
	public String create_Doctor_Employee_User(BigDecimal charges, String address, String emgContact, String firstName,
			String lastName, String idUser, String password, String role) {

		EntityManager em = this.getEm();
		String result = "success";
		try {

			User user = new User(idUser, password, role);
			Employee employee = new Employee(address, emgContact, firstName, user, lastName);
			Doctor doctor = new Doctor(charges, employee);

			em.getTransaction().begin();
			em.persist(user);
			em.persist(employee);
			em.persist(doctor);
			em.getTransaction().commit();
		} catch (Exception e) {
			result = e.getMessage();
			em.getTransaction().rollback();
		}
		return result;

	}

}
